<?php
namespace app\index\controller;
use think\Db;
class Cases extends Base{
    public function cases(){
        $type=input('get.type');
        $map = [
            ['state', '=', 1]
        ];
        if($type>0){
            array_push($map,['type','=',$type]);
        }
    	$cases_banner=Db::name('cases_banner')->where('id',1)->find();
        $headquarters_branches=Db::name('headquarters_branches')->order('sort desc')->select();
        $others=Db::name('others')->where('state',1)->order('sort desc')->select();
        $cases=Db::name('cases')->where($map)->order('id desc')->limit(12)->select();
        $this->assign('system_set',$this->system_set());
		$this->assign('cases_banner',$cases_banner);
        $this->assign('tmdurl',$this->request->domain());
        $this->assign('others',$others);

        $this->assign('headquarters_branches',$headquarters_branches);
        $this->assign('cases',$cases);
        $this->assign('type',$type);
        return $this->fetch();
    }

    public function cases_page()
    {
        $page=input("param.page");
        $type=input('param.type');
        $map = [
            ['state', '=', 1]
        ];
        if($type>0){
            array_push($map,['type','=',$type]);
        }
        $data=Db::name('cases')->where($map)->order('id desc')->limit(12*($page-1),12)->select();
        $count=Db::name('cases')->where($map)->count();
        if($data){
            return json(['code' => 200, 'msg' => '成功','page'=>$page,'pages'=>ceil($count/12),'data'=>$data]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据']);
        }
    }

    public function case_detail(){
        $param = input('get.');
        $type=input('param.type');
        $map = [
            ['state', '=', 1]
        ];
        if($type>0){
            array_push($map,['type','=',$type]);
        }
        $headquarters_branches=Db::name('headquarters_branches')->order('sort desc')->select();
        $others=Db::name('others')->where('state',1)->order('sort desc')->select();
        $data=Db::name('cases')->where('id',$param['id'])->find();
        $cases_banner=Db::name('cases_banner')->where('id',1)->find();

        $pre=Db::name('cases')->where('id','<',$param['id'])->where($map)->order('id desc')->find();
        $next=Db::name('cases')->where('id','>',$param['id'])->where($map)->find();

        $this->assign('system_set',$this->system_set());
        $this->assign('tmdurl',$this->request->domain());
        $this->assign('others',$others);
        $this->assign('headquarters_branches',$headquarters_branches);
        $this->assign('data',$data);
        $this->assign('cases_banner',$cases_banner);
        $this->assign('pre',$pre);
        $this->assign('next',$next);
        $this->assign('type',$type);
        return $this->fetch();
    }
}
